/*      */ package com.newcrm.webapi;
/*      */ 
/*      */ import com.newcrm.bean.CompanyInfo;
/*      */ import com.newcrm.bean.CustomerInfo;
/*      */ import com.newcrm.bean.User;
/*      */ import com.newcrm.db.CompanyExtManager;
/*      */ import com.newcrm.db.CustomerCategoryManager;
/*      */ import com.newcrm.db.CustomerFieldManager;
/*      */ import com.newcrm.db.CustomerManager;
/*      */ import com.newcrm.db.DealCategoryManager;
/*      */ import com.newcrm.db.GroupManager;
/*      */ import com.newcrm.db.NoteDefaultWordManager;
/*      */ import com.newcrm.db.TagManager;
/*      */ import com.newcrm.db.TaskCategoryManager;
/*      */ import com.newcrm.db.TaskManager;
/*      */ import com.newcrm.db.UserManager;
/*      */ import com.newcrm.exception.CaptchaException;
/*      */ import com.newcrm.exception.DBException;
/*      */ import com.newcrm.exception.NotFoundException;
/*      */ import com.newcrm.manager.CompanyStatusManager;
/*      */ import com.newcrm.util.ParameterUtil;
/*      */ import com.newcrm.util.Util;
/*      */ import com.newcrm.webapi.sub.ShowDeal;
/*      */ import java.io.ByteArrayOutputStream;
/*      */ import java.io.IOException;
/*      */ import java.io.OutputStream;
/*      */ import java.io.PrintWriter;
/*      */ import java.sql.SQLException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import javax.servlet.RequestDispatcher;
/*      */ import javax.servlet.ServletContext;
/*      */ import javax.servlet.ServletException;
/*      */ import javax.servlet.http.Cookie;
/*      */ import javax.servlet.http.HttpServletRequest;
/*      */ import javax.servlet.http.HttpServletResponse;
/*      */ import javax.servlet.http.HttpSession;
/*      */ import net.glxn.qrgen.core.AbstractQRCode;
/*      */ import net.glxn.qrgen.core.image.ImageType;
/*      */ import net.glxn.qrgen.javase.QRCode;
/*      */ import net.sf.json.JSONObject;
/*      */ import org.apache.log4j.Logger;
/*      */ 
/*      */ public class Mobile extends AbstractHttpServlet
/*      */ {
/*      */   private static final long serialVersionUID = 279937483161680021L;
/*   55 */   private static Logger logger = Logger.getLogger(Mobile.class.getName());
/*      */ 
/*      */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*      */     throws ServletException, IOException
/*      */   {
/*   79 */     String url = request.getRequestURI();
/*   80 */     String[] param = ParameterUtil.getParameters(url, "/m");
/*      */ 
/*   83 */     if ((param == null) || (param.length == 0)) {
/*   84 */       if (checkSession(request)) {
/*   85 */         request.getRequestDispatcher("/mobileV2/index.jsp").forward(
/*   86 */           request, response);
/*   87 */         return;
/*      */       }
/*   89 */       request.getRequestDispatcher("/mobileV2/login.jsp").forward(
/*   90 */         request, response);
/*   91 */       return;
/*      */     }
/*      */ 
/*   94 */     if (param.length == 1)
/*      */     {
/*   96 */       if ("login".equals(param[0])) {
/*   97 */         request.getRequestDispatcher("/mobileV2/login.jsp").forward(
/*   98 */           request, response);
/*   99 */         return;
/*      */       }
/*      */ 
/*  103 */       if ("doLogin".equals(param[0])) {
/*  104 */         response.setContentType("text/html;charset=utf-8");
/*  105 */         PrintWriter out = response.getWriter();
/*  106 */         JSONObject jsonObject = new JSONObject();
/*      */ 
/*  108 */         if (!CompanyStatusManager.getInstance().getLoginStatus()) {
/*  109 */           jsonObject.put("error", "1");
/*  110 */           jsonObject.put("msg", "用户名或密码错误");
/*  111 */           out.println(jsonObject);
/*  112 */           out.flush();
/*  113 */           out.close();
/*  114 */           return;
/*      */         }
/*      */         try
/*      */         {
/*  118 */           boolean flag = doLogin(request, response);
/*  119 */           jsonObject.put("error", flag ? "0" : "1");
/*  120 */           if (!flag)
/*  121 */             jsonObject.put("msg", "用户名或密码错误");
/*      */         }
/*      */         catch (CaptchaException e) {
/*  124 */           jsonObject.put("error", "1");
/*  125 */           jsonObject.put("msg", "验证码输入错误");
/*      */         }
/*      */ 
/*  128 */         out.println(jsonObject);
/*  129 */         out.flush();
/*  130 */         out.close();
/*  131 */         return;
/*      */       }
/*      */ 
/*  135 */       if ("qrcode".equals(param[0])) {
/*  136 */         showQRCode(request, response);
/*  137 */         return;
/*      */       }
/*      */     }
/*      */ 
/*  141 */     if (!checkSession(request)) {
/*  142 */       response.sendRedirect(request.getContextPath() + "/m/login");
/*  143 */       return;
/*      */     }
/*      */ 
/*  146 */     if (param.length == 1)
/*      */     {
/*  148 */       if ("index".equals(param[0])) {
/*  149 */         request.getRequestDispatcher("/mobileV2/index.jsp").forward(
/*  150 */           request, response);
/*  151 */         return;
/*      */       }
/*      */ 
/*  154 */       if ("customer".equals(param[0])) {
/*  155 */         int customerId = Util.getURLIntParameter(request
/*  156 */           .getParameter("id"));
/*      */ 
/*  158 */         CustomerManager manager = new CustomerManager();
/*      */ 
/*  160 */         CustomerInfo customerInfo = null;
/*  161 */         if (customerId > 0)
/*      */           try {
/*  163 */             customerInfo = manager.getCustomerInfo(customerId, 
/*  164 */               true, true);
/*      */           }
/*      */           catch (DBException localDBException) {
/*      */           }
/*  168 */         User user = getLoginUser(request);
/*  169 */         if ((customerInfo == null) || 
/*  171 */           (!customerInfo.hasViewRight(user, 
/*  171 */           super.getCompany(user.companyId)))) {
/*  172 */           request.getRequestDispatcher(
/*  173 */             "/mobileV2/customerDetail-404.jsp").forward(
/*  174 */             request, response);
/*  175 */           return;
/*      */         }
/*      */ 
/*  178 */         request.setAttribute("customerInfo", customerInfo);
/*      */ 
/*  180 */         CustomerFieldManager customerFieldManager = new CustomerFieldManager();
/*  181 */         List fields = customerFieldManager.list(
/*  182 */           user.companyId, 1);
/*  183 */         request.setAttribute("fields", fields);
/*      */ 
/*  186 */         List peoples = new ArrayList();
/*  187 */         if (customerInfo.type == 0) {
/*      */           try {
/*  189 */             peoples = manager.getPeoples(customerInfo.id);
/*      */           } catch (Exception e) {
/*  191 */             logger.warn("get company peoples failed", e);
/*      */           }
/*      */         }
/*      */ 
/*  195 */         request.setAttribute("peoples", peoples);
/*  196 */         request.getRequestDispatcher("/mobileV2/customerDetail.jsp")
/*  197 */           .forward(request, response);
/*  198 */         return;
/*      */       }
/*      */ 
/*  202 */       if ("deal".equals(param[0])) {
/*  203 */         String dealId = 
/*  204 */           Util.getURLParameter(request.getParameter("id"));
/*      */         try {
/*  206 */           new ShowDeal(this, dealId, true).parse(request, response);
/*      */         } catch (NotFoundException e) {
/*  208 */           request.getRequestDispatcher("/mobileV2/dealDetail-404.jsp")
/*  209 */             .forward(request, response);
/*      */         }
/*      */       }
/*      */ 
/*  213 */       if ("customerDetail".equals(param[0])) {
/*  214 */         String customerId = Util.getURLParameter(request
/*  215 */           .getParameter("id"));
/*      */ 
/*  217 */         if ((customerId != null) && (customerId.length() > 0)) {
/*  218 */           request.setAttribute("id", customerId);
/*  219 */           request.getRequestDispatcher(
/*  220 */             "/mobileV2/customerContactInfo.jsp").forward(
/*  221 */             request, response);
/*  222 */           return;
/*      */         }
/*      */       }
/*      */ 
/*  226 */       if ("customerEditDetail".equals(param[0])) {
/*  227 */         String customerId = Util.getURLParameter(request
/*  228 */           .getParameter("id"));
/*      */ 
/*  230 */         if ((customerId != null) && (customerId.length() > 0)) {
/*  231 */           request.setAttribute("id", customerId);
/*  232 */           request.getRequestDispatcher(
/*  233 */             "/mobileV2/customerEditContactInfo.jsp").forward(
/*  234 */             request, response);
/*  235 */           return;
/*      */         }
/*      */       }
/*      */ 
/*  239 */       if ("note".equals(param[0]))
/*      */       {
/*  241 */         String noteId = 
/*  242 */           Util.getURLParameter(request.getParameter("id"));
/*  243 */         if ((noteId != null) && (noteId.length() > 0)) {
/*  244 */           request.setAttribute("id", noteId);
/*  245 */           request.getRequestDispatcher("/mobileV2/noteDetail.jsp")
/*  246 */             .forward(request, response);
/*  247 */           return;
/*      */         }
/*      */       }
/*      */ 
/*  251 */       if ("tasks".equals(param[0])) {
/*  252 */         request.getRequestDispatcher("/mobileV2/task.jsp").forward(
/*  253 */           request, response);
/*  254 */         return;
/*      */       }
/*      */ 
/*  257 */       if ("task".equals(param[0])) {
/*  258 */         String id = Util.getURLParameter(request.getParameter("id"));
/*  259 */         request.setAttribute("id", id);
/*  260 */         request.getRequestDispatcher("/mobileV2/taskDetail.jsp")
/*  261 */           .forward(request, response);
/*  262 */         return;
/*      */       }
/*      */     }
/*      */ 
/*  266 */     if (param.length == 2)
/*      */     {
/*  268 */       if (("task".equals(param[0])) && ("new".equals(param[1]))) {
/*  269 */         addTask(request, response);
/*  270 */         return;
/*      */       }
/*      */ 
/*  273 */       if (("task".equals(param[0])) && ("edit".equals(param[1]))) {
/*  274 */         String id = Util.getURLParameter(request.getParameter("id"));
/*      */ 
/*  276 */         request.setAttribute("id", id);
/*  277 */         request.getRequestDispatcher("/mobileV2/editTask.jsp").forward(
/*  278 */           request, response);
/*  279 */         return;
/*      */       }
/*      */ 
/*  284 */       if (("customer".equals(param[0])) && ("new".equals(param[1]))) {
/*  285 */         addCustomer(request, response);
/*  286 */         return;
/*      */       }
/*      */ 
/*  290 */       if (("customer".equals(param[0])) && ("people".equals(param[1]))) {
/*  291 */         String id = Util.getURLParameter(request.getParameter("id"));
/*  292 */         request.setAttribute("id", id);
/*  293 */         request.getRequestDispatcher("/mobile/customerPersonDetail.jsp")
/*  294 */           .forward(request, response);
/*  295 */         return;
/*      */       }
/*      */ 
/*  299 */       if (("customer".equals(param[0])) && ("updateName".equals(param[1]))) {
/*  300 */         updateCustomerName(request, response);
/*  301 */         return;
/*      */       }
/*      */ 
/*  305 */       if (("customer".equals(param[0])) && ("updateTitle".equals(param[1]))) {
/*  306 */         updateCustomerTitle(request, response);
/*  307 */         return;
/*      */       }
/*      */ 
/*  311 */       if (("customerExtInfo".equals(param[0])) && ("update".equals(param[1]))) {
/*  312 */         updateCustomerExtInfo(request, response);
/*  313 */         return;
/*      */       }
/*      */ 
/*  317 */       if (("customerExtInfo".equals(param[0])) && ("delete".equals(param[1]))) {
/*  318 */         deleteCustomerExtInfo(request, response);
/*  319 */         return;
/*      */       }
/*      */ 
/*  323 */       if (("customerExtInfo".equals(param[0])) && ("new".equals(param[1]))) {
/*  324 */         addCustomerExtInfo(request, response);
/*  325 */         return;
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   private void showQRCode(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  342 */     String url = request.getParameter("url");
/*      */     try {
/*  344 */       ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG)
/*  345 */         .withSize(200, 200).stream();
/*      */ 
/*  347 */       response.setContentType("image/png");
/*  348 */       response.setContentLength(out.size());
/*      */ 
/*  350 */       OutputStream outStream = response.getOutputStream();
/*  351 */       outStream.write(out.toByteArray());
/*  352 */       outStream.flush();
/*  353 */       outStream.close();
/*      */     } catch (Exception e) {
/*  355 */       logger.warn("create QRCode failed", e);
/*      */     }
/*      */   }
/*      */ 
/*      */   private void addTask(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  368 */     User user = getLoginUser(request);
/*      */ 
/*  370 */     String taskTitle = Util.getURLParameter(request
/*  371 */       .getParameter("TaskTitle"));
/*  372 */     int due = Util.str2Int(request.getParameter("Due"));
/*  373 */     String responsibleUserId = Util.getURLParameter(request
/*  374 */       .getParameter("ResponsibleUserId"));
/*  375 */     int categoryId = Util.str2Int(request.getParameter("CategoryId"));
/*  376 */     int taskVisible = Util.str2Int(request.getParameter("TaskVisible"));
/*      */ 
/*  378 */     String referenceUrl = Util.getURLParameter(request
/*  379 */       .getParameter("ReferenceUrl"));
/*  380 */     String referenceTitle = Util.getURLParameter(request
/*  381 */       .getParameter("ReferenceTitle"));
/*  382 */     int referenceId = Util.str2Int(request.getParameter("ReferenceId"));
/*  383 */     int referenceType = Util.str2Int(request.getParameter("ReferenceType"));
/*      */ 
/*  385 */     response.setContentType("text/html;charset=utf-8");
/*  386 */     PrintWriter out = response.getWriter();
/*  387 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  389 */     TaskManager manager = new TaskManager();
/*      */     try {
/*  391 */       int taskId = manager.add(taskTitle, Util.getDueDate(due), 
/*  392 */         responsibleUserId, categoryId, taskVisible, referenceUrl, 
/*  393 */         referenceTitle, user.companyId, referenceId, referenceType, 
/*  394 */         user.userId);
/*  395 */       if (taskId > 0) {
/*  396 */         jsonObject.put("error", "0");
/*  397 */         jsonObject.put("taskId", Integer.valueOf(taskId));
/*      */       } else {
/*  399 */         jsonObject.put("error", "1");
/*      */       }
/*      */     } catch (DBException e) {
/*  402 */       jsonObject.put("error", "1");
/*      */     }
/*  404 */     out.println(jsonObject);
/*  405 */     out.flush();
/*  406 */     out.close();
/*      */   }
/*      */ 
/*      */   private void addCustomerExtInfo(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  418 */     User user = getLoginUser(request);
/*      */ 
/*  420 */     String info1 = Util.getURLParameter(request.getParameter("info1"));
/*  421 */     String info2 = Util.getURLParameter(request.getParameter("info2"));
/*  422 */     int filedId = Util.getURLIntParameter(request.getParameter("filedId"));
/*  423 */     int customerId = Util.getURLIntParameter(request
/*  424 */       .getParameter("customerId"));
/*      */ 
/*  426 */     response.setContentType("text/html;charset=utf-8");
/*  427 */     PrintWriter out = response.getWriter();
/*  428 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  430 */     if ((filedId <= 0) || (customerId <= 0) || (info1 == null) || 
/*  431 */       (info1.trim().length() == 0)) {
/*  432 */       jsonObject.put("error", "1");
/*  433 */       out.println(jsonObject);
/*  434 */       out.flush();
/*  435 */       out.close();
/*  436 */       return;
/*      */     }
/*      */ 
/*  440 */     if (!checkCustomerModifyRight(customerId, user)) {
/*  441 */       jsonObject.put("error", "1");
/*  442 */       out.println(jsonObject);
/*  443 */       out.flush();
/*  444 */       out.close();
/*  445 */       return;
/*      */     }
/*      */ 
/*  448 */     CustomerManager manager = new CustomerManager();
/*      */     try
/*      */     {
/*  451 */       int customerExtId = manager.addCustomerExtInfo(info1, info2, null, 
/*  452 */         null, customerId, user.companyId, filedId);
/*  453 */       jsonObject.put("error", "0");
/*  454 */       jsonObject.put("customerExtId", Integer.valueOf(customerExtId));
/*      */     } catch (DBException e) {
/*  456 */       jsonObject.put("error", "1");
/*  457 */       logger.warn("update customer extInfo failed", e);
/*      */     }
/*  459 */     out.println(jsonObject);
/*  460 */     out.flush();
/*  461 */     out.close();
/*      */   }
/*      */ 
/*      */   private void updateCustomerExtInfo(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  473 */     User user = getLoginUser(request);
/*      */ 
/*  475 */     String info1 = Util.getURLParameter(request.getParameter("info1"));
/*  476 */     String info2 = Util.getURLParameter(request.getParameter("info2"));
/*  477 */     int customerExtId = Util.getURLIntParameter(request
/*  478 */       .getParameter("customerExtId"));
/*  479 */     int customerId = Util.getURLIntParameter(request
/*  480 */       .getParameter("customerId"));
/*      */ 
/*  482 */     response.setContentType("text/html;charset=utf-8");
/*  483 */     PrintWriter out = response.getWriter();
/*  484 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  486 */     if ((customerExtId <= 0) || (info1 == null) || (customerId <= 0) || 
/*  487 */       (info1.trim().length() == 0)) {
/*  488 */       jsonObject.put("error", "1");
/*  489 */       out.println(jsonObject);
/*  490 */       out.flush();
/*  491 */       out.close();
/*  492 */       return;
/*      */     }
/*      */ 
/*  496 */     if (!checkCustomerModifyRight(customerId, user)) {
/*  497 */       jsonObject.put("error", "1");
/*  498 */       out.println(jsonObject);
/*  499 */       out.flush();
/*  500 */       out.close();
/*  501 */       return;
/*      */     }
/*      */ 
/*  504 */     CustomerManager manager = new CustomerManager();
/*      */     try
/*      */     {
/*  507 */       manager.updateCustomerExtInfo(info1, info2, null, null, 
/*  508 */         customerExtId);
/*  509 */       jsonObject.put("error", "0");
/*      */     } catch (DBException e) {
/*  511 */       jsonObject.put("error", "1");
/*  512 */       logger.warn("update customer extInfo failed", e);
/*      */     }
/*  514 */     out.println(jsonObject);
/*  515 */     out.flush();
/*  516 */     out.close();
/*      */   }
/*      */ 
/*      */   private void deleteCustomerExtInfo(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  528 */     User user = getLoginUser(request);
/*      */ 
/*  530 */     int customerExtId = Util.getURLIntParameter(request
/*  531 */       .getParameter("customerExtId"));
/*  532 */     int customerId = Util.getURLIntParameter(request
/*  533 */       .getParameter("customerId"));
/*      */ 
/*  535 */     response.setContentType("text/html;charset=utf-8");
/*  536 */     PrintWriter out = response.getWriter();
/*  537 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  539 */     if ((customerExtId <= 0) || (customerId <= 0)) {
/*  540 */       jsonObject.put("error", "1");
/*  541 */       out.println(jsonObject);
/*  542 */       out.flush();
/*  543 */       out.close();
/*  544 */       return;
/*      */     }
/*      */ 
/*  548 */     if (!checkCustomerModifyRight(customerId, user)) {
/*  549 */       jsonObject.put("error", "1");
/*  550 */       out.println(jsonObject);
/*  551 */       out.flush();
/*  552 */       out.close();
/*  553 */       return;
/*      */     }
/*      */ 
/*  556 */     CustomerManager manager = new CustomerManager();
/*      */     try
/*      */     {
/*  559 */       manager.deleteCustomerExtInfo(customerExtId);
/*  560 */       jsonObject.put("error", "0");
/*      */     } catch (DBException e) {
/*  562 */       jsonObject.put("error", "1");
/*  563 */       logger.warn("delete customer extInfo failed", e);
/*      */     }
/*  565 */     out.println(jsonObject);
/*  566 */     out.flush();
/*  567 */     out.close();
/*      */   }
/*      */ 
/*      */   private void updateCustomerName(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  579 */     User user = getLoginUser(request);
/*      */ 
/*  581 */     String customerName = Util.getURLParameter(request
/*  582 */       .getParameter("customerName"));
/*  583 */     int customerId = Util.getURLIntParameter(request
/*  584 */       .getParameter("customerId"));
/*      */ 
/*  586 */     response.setContentType("text/html;charset=utf-8");
/*  587 */     PrintWriter out = response.getWriter();
/*  588 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  590 */     if ((customerId <= 0) || (customerName == null) || 
/*  591 */       (customerName.trim().length() == 0)) {
/*  592 */       jsonObject.put("error", "1");
/*  593 */       out.println(jsonObject);
/*  594 */       out.flush();
/*  595 */       out.close();
/*  596 */       return;
/*      */     }
/*      */ 
/*  600 */     if (!checkCustomerModifyRight(customerId, user)) {
/*  601 */       jsonObject.put("error", "1");
/*  602 */       out.println(jsonObject);
/*  603 */       out.flush();
/*  604 */       out.close();
/*  605 */       return;
/*      */     }
/*      */ 
/*  608 */     CustomerManager manager = new CustomerManager();
/*      */ 
/*  610 */     manager.updateCustomerName(customerId, customerName, user.userId);
/*  611 */     jsonObject.put("error", "0");
/*  612 */     out.println(jsonObject);
/*  613 */     out.flush();
/*  614 */     out.close();
/*      */   }
/*      */ 
/*      */   private void updateCustomerTitle(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  626 */     User user = getLoginUser(request);
/*      */ 
/*  628 */     String title = Util.getURLParameter(request
/*  629 */       .getParameter("customerTitle"));
/*  630 */     int customerId = Util.getURLIntParameter(request
/*  631 */       .getParameter("customerId"));
/*      */ 
/*  633 */     response.setContentType("text/html;charset=utf-8");
/*  634 */     PrintWriter out = response.getWriter();
/*  635 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  637 */     if (customerId <= 0) {
/*  638 */       jsonObject.put("error", "1");
/*  639 */       out.println(jsonObject);
/*  640 */       out.flush();
/*  641 */       out.close();
/*  642 */       return;
/*      */     }
/*      */ 
/*  646 */     if (!checkCustomerModifyRight(customerId, user)) {
/*  647 */       jsonObject.put("error", "1");
/*  648 */       out.println(jsonObject);
/*  649 */       out.flush();
/*  650 */       out.close();
/*  651 */       return;
/*      */     }
/*      */ 
/*  654 */     CustomerManager manager = new CustomerManager();
/*      */ 
/*  656 */     manager.updateCustomerTitle(customerId, title, user.userId);
/*  657 */     jsonObject.put("error", "0");
/*  658 */     out.println(jsonObject);
/*  659 */     out.flush();
/*  660 */     out.close();
/*      */   }
/*      */ 
/*      */   private void addCustomer(HttpServletRequest request, HttpServletResponse response)
/*      */     throws IOException
/*      */   {
/*  672 */     User user = getLoginUser(request);
/*  673 */     String name = Util.getURLParameter(request.getParameter("name"));
/*  674 */     int type = Util.getURLIntParameter(request.getParameter("type"));
/*  675 */     int parentCustomerId = Util.getURLIntParameter(request
/*  676 */       .getParameter("parentCustomerId"));
/*      */ 
/*  678 */     response.setContentType("text/html;charset=utf-8");
/*  679 */     PrintWriter out = response.getWriter();
/*  680 */     JSONObject jsonObject = new JSONObject();
/*      */ 
/*  682 */     if ((name == null) || (name.trim().length() == 0)) {
/*  683 */       jsonObject.put("error", "1");
/*  684 */       out.println(jsonObject);
/*  685 */       out.flush();
/*  686 */       out.close();
/*  687 */       return;
/*      */     }
/*      */ 
/*  690 */     CustomerManager manager = new CustomerManager();
/*      */     try
/*      */     {
/*  693 */       int customerId = manager.addCustomer(parentCustomerId, name, type, 
/*  694 */         user.userId, user.companyId);
/*  695 */       if (customerId < 0)
/*      */       {
/*  697 */         jsonObject.put("error", "2");
/*  698 */         jsonObject.put("customerId", Integer.valueOf(customerId * -1));
/*  699 */       } else if (customerId == 0) {
/*  700 */         jsonObject.put("error", "1");
/*      */       } else {
/*  702 */         jsonObject.put("error", "0");
/*  703 */         jsonObject.put("customerId", Integer.valueOf(customerId));
/*      */       }
/*      */     } catch (DBException e) {
/*  706 */       jsonObject.put("error", "1");
/*  707 */       logger.warn("update customer extInfo failed", e);
/*      */     }
/*  709 */     out.println(jsonObject);
/*  710 */     out.flush();
/*  711 */     out.close();
/*      */   }
/*      */ 
/*      */   private boolean doLogin(HttpServletRequest request, HttpServletResponse response)
/*      */     throws CaptchaException
/*      */   {
/*  726 */     String userId = Util.getURLParameter(request.getParameter("userId"));
/*  727 */     String password = 
/*  728 */       Util.getURLParameter(request.getParameter("password"));
/*  729 */     String captcha = Util.getURLParameter(request.getParameter("captcha"));
/*      */ 
/*  731 */     if ((userId == null) || (userId.trim().length() == 0)) {
/*  732 */       logger.warn("h5 login failed, userId is null");
/*  733 */       return false;
/*      */     }
/*      */ 
/*  736 */     if ((password == null) || (password.trim().length() == 0)) {
/*  737 */       logger.warn("h5 login failed, password is null");
/*  738 */       return false;
/*      */     }
/*      */ 
/*  741 */     if ((captcha == null) || (captcha.trim().length() == 0)) {
/*  742 */       logger.warn("h5 login failed, captcha is null");
/*  743 */       return false;
/*      */     }
/*      */ 
/*  748 */     String ip = Util.getClientIpAddr(request);
/*      */ 
/*  750 */     if (!Util.checkCode(request, captcha)) {
/*  751 */       logger.warn("登录IP：" + ip + "， 验证码输入错误。 userId: " + userId + 
/*  752 */         " / captcha: " + captcha);
/*  753 */       throw new CaptchaException();
/*      */     }
/*      */ 
/*  757 */     UserManager manager = new UserManager();
/*      */ 
/*  759 */     Integer companyId = null;
/*      */     try {
/*  761 */       Map map = manager.login(userId, password, false);
/*  762 */       companyId = (Integer)map.get("companyId");
/*      */     } catch (Exception e) {
/*  764 */       logger.warn("login failed", e);
/*      */     }
/*  766 */     if ((companyId == null) || (companyId.intValue() < 0)) {
/*  767 */       return false;
/*      */     }
/*      */     try
/*      */     {
/*  771 */       manager.updateLogin(userId, ip);
/*      */ 
/*  774 */       Cookie cookie = new Cookie("userId", userId);
/*  775 */       cookie.setMaxAge(31536000);
/*  776 */       response.addCookie(cookie);
/*      */     } catch (Exception e) {
/*  778 */       logger.warn("update Session key failed", e);
/*      */     }
/*      */ 
/*  781 */     logger.warn("H5 登录成功，IP：" + ip + "， 登录用户ID：" + userId);
/*      */ 
/*  784 */     Map companys = (Map)getServletContext()
/*  785 */       .getAttribute("companys");
/*      */ 
/*  787 */     if (companys == null) {
/*  788 */       companys = new HashMap();
/*  789 */       getServletContext().setAttribute("companys", 
/*  790 */         companys);
/*      */     }
/*      */ 
/*  793 */     CompanyInfo company = (CompanyInfo)companys.get(companyId);
/*  794 */     if (company == null) {
/*  795 */       company = new CompanyInfo();
/*  796 */       company.id = companyId.intValue();
/*  797 */       companys.put(companyId, company);
/*      */     }CustomerCategoryManager customerCategoryManager;
/*      */     try {
/*  800 */       if (company.users == null)
/*      */       {
/*  802 */         company.users = manager.getCompanyUsers(companyId.intValue(), 
/*  803 */           null);
/*      */ 
/*  806 */         TaskCategoryManager taskCategoryManager = new TaskCategoryManager();
/*  807 */         DealCategoryManager dealCategoryManager = new DealCategoryManager();
/*  808 */         customerCategoryManager = new CustomerCategoryManager();
/*  809 */         NoteDefaultWordManager noteDefaultWordManager = new NoteDefaultWordManager();
/*  810 */         TagManager tagManager = new TagManager();
/*  811 */         GroupManager groupManager = new GroupManager();
/*  812 */         CompanyExtManager companyExtManager = new CompanyExtManager();
/*      */         try {
/*  814 */           company.taskCategorys = taskCategoryManager.list(companyId
/*  815 */             .intValue());
/*  816 */           company.dealCategorys = dealCategoryManager.list(companyId
/*  817 */             .intValue());
/*  818 */           company.customerCategorys = customerCategoryManager.list(
/*  819 */             companyId.intValue(), 1);
/*  820 */           company.setGroups(groupManager.list(companyId.intValue()));
/*  821 */           company.noteDefaultWords = noteDefaultWordManager
/*  822 */             .list(companyId.intValue());
/*  823 */           company.tags = tagManager.list(companyId.intValue());
/*      */ 
/*  826 */           company.closeGroupPublicDataFlag = companyExtManager
/*  827 */             .getFieldValue(
/*  828 */             companyId.intValue(), 
/*  829 */             12);
/*      */ 
/*  832 */           company.setPublicReadOnlyFlag = companyExtManager
/*  833 */             .getFieldValue(companyId.intValue(), 
/*  834 */             13);
/*      */ 
/*  836 */           String allowGroupList = companyExtManager.getFieldValue(
/*  837 */             companyId.intValue(), 
/*  838 */             11);
/*      */ 
/*  840 */           if ((allowGroupList == null) || 
/*  841 */             (allowGroupList.trim().length() == 0))
/*      */           {
/*  843 */             allowGroupList = "*";
/*      */           }
/*  845 */           company.allowPublicGroupList = allowGroupList;
/*      */         }
/*      */         catch (SQLException e) {
/*  848 */           logger.warn("get company task/deal/group category failed", 
/*  849 */             e);
/*      */         }
/*      */       }
/*      */     } catch (DBException e) {
/*  853 */       logger.warn("get company users failed", e);
/*  854 */       return false;
/*      */     }
/*  856 */     User currentUser = null;
/*  857 */     for (User user : company.users) {
/*  858 */       if (userId.equals(user.userId)) {
/*  859 */         currentUser = user;
/*  860 */         break;
/*      */       }
/*      */     }
/*      */ 
/*  864 */     if (currentUser == null) {
/*  865 */       return false;
/*      */     }
/*      */ 
/*  868 */     HttpSession session = request.getSession(true);
/*  869 */     session.setAttribute("user", currentUser);
/*      */ 
/*  871 */     return true;
/*      */   }
/*      */ 
/*      */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*      */     throws ServletException, IOException
/*      */   {
/*  891 */     doGet(request, response);
/*      */   }
/*      */ 
/*      */   private boolean checkSession(HttpServletRequest request)
/*      */   {
/*  900 */     HttpSession session = request.getSession(false);
/*  901 */     if (session == null) {
/*  902 */       logger.warn("session is null");
/*  903 */       return false;
/*      */     }
/*  905 */     Object obj = session.getAttribute("user");
/*  906 */     if (obj == null) {
/*  907 */       logger.warn("session is null");
/*  908 */       return false;
/*      */     }
/*      */ 
/*  911 */     if (!(obj instanceof User)) {
/*  912 */       logger.warn("session is null");
/*  913 */       return false;
/*      */     }
/*      */ 
/*  916 */     return true;
/*      */   }
/*      */ 
/*      */   private boolean checkCustomerModifyRight(int customerId, User user)
/*      */   {
/*  927 */     if (user.isAdmin) {
/*  928 */       return true;
/*      */     }
/*      */ 
/*  931 */     CustomerManager manager = new CustomerManager();
/*      */     try
/*      */     {
/*  934 */       CustomerInfo customerInfo = manager.getCustomerMiniInfo(customerId);
/*  935 */       if (customerInfo.visible == 1) {
/*  936 */         return true;
/*      */       }
/*      */ 
/*  939 */       if ((customerInfo.visible == 4) && 
/*  940 */         (user.userId.equals(customerInfo.permissionsId)))
/*  941 */         return true;
/*      */     }
/*      */     catch (DBException e) {
/*  944 */       return false;
/*      */     }
/*      */     CustomerInfo customerInfo;
/*  947 */     return false;
/*      */   }
/*      */ 
/*      */   protected String getTagkey()
/*      */   {
/*  953 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getAddAction()
/*      */   {
/*  959 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getListAction()
/*      */   {
/*  965 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getShowAddPageAction()
/*      */   {
/*  971 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getUpdateAction(String id)
/*      */   {
/*  977 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getShowConfirmDestroyAction(String id)
/*      */   {
/*  983 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getDeleteAction(String id)
/*      */   {
/*  989 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getShowAction(String id)
/*      */   {
/*  995 */     return null;
/*      */   }
/*      */ 
/*      */   protected AbstractAction getShowUpdatePageAction(String id)
/*      */   {
/* 1001 */     return null;
/*      */   }
/*      */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.Mobile
 * JD-Core Version:    0.6.1
 */